﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //查找
            //输入框
            var name = textBox1.Text;
            //数据库模糊查询代码
            var sql = string.Format("select * from Artycles where Title  like '%{0}%' ", name);
            //连接数据库
            var dt = DnHelper.GetDataTable(sql);

            dataGridView1.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //更新
            //var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;

            //var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;

            //var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;

            //var createdTime = (string)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;
            //Form4 form4 = new Form4(id, title, author,createdTime);
            //var res = form4.ShowDialog();
            //if (res == DialogResult.Yes)
            //{
            //    var name = textBox1.Text;

            //    var sqlStu = string.Format("select * from Artycles where Title like '%{0}%'", name);

            //    var dt = DnHelper.AddOrEditOrDeleteSave(sqlStu);
            //    dataGridView1.DataSource = dt;

            //    var data = DnHelper.GetDataTable(sqlStu);
            //    dataGridView1.DataSource = sqlStu;
            //}
            //else
            //{
            //    MessageBox.Show("未选择任何行");
            //}
            if (dataGridView1.SelectedRows.Count > 0)
            {  //取值
                var title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var createdTime = (string)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;
                var id = (int)dataGridView1.SelectedRows[0].Cells["id"].Value;
                Form4 add = new Form4(id,title,author,createdTime);
                var res = add.ShowDialog();
                if (res == DialogResult.Yes)
                {
                    string sql = "select * from Artycles";
                    var datatable = DnHelper.GetDataTable(sql);
                    dataGridView1.DataSource = datatable;
                }
            }
            else
            {
                MessageBox.Show("未选择任何数据！", "提示");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //添加
            //Form4 form4 = new Form4();

            //var res = form4.ShowDialog();

            //if (res == DialogResult.Yes)
            //{
            //    var name = textBox1.Text;
            //    var sqlStu = string.Format("select * from Artycles where Title like '%{0}%'", name);
            //    var dt = DnHelper.AddOrEditOrDeleteSave(sqlStu);
            //    dataGridView1.DataSource = dt;
            //}
            //else
            //{
            //    MessageBox.Show("This is NO ");
            //}
            Form4 form4 = new Form4();
            var bgi = form4.ShowDialog();
            if (bgi == DialogResult.Yes)
            {
                string sql = "select * from Artycles";
                var databale = DnHelper.GetDataTable(sql);
                dataGridView1.DataSource = databale;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //删除
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];

                var id = (int)row.Cells["Id"].Value;

                var res = MessageBox.Show("确定删除？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {

                    var sql = string.Format("delete from Artycles where Id={0}", id);

                    var rowCount = DnHelper.AddOrEditOrDeleteSave(sql);

                    if (rowCount > 0)
                    {
                        var title = textBox1.Text;

                        string selectStr = string.Format("select * from Artycles where Title like '%{0}%'", title);

                        var dataTable = DnHelper.GetDataTable(selectStr);

                        dataGridView1.DataSource = dataTable;

                        MessageBox.Show("删除成功", "提示");
                    }
                }
            }
        }

            private void Form3_Load(object sender, EventArgs e)
            {
                string cmdString = "select * from Artycles";

                var dt = DnHelper.GetDataTable(cmdString);

                dataGridView1.DataSource = dt;

                dataGridView1.ReadOnly = true;

                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

                dataGridView1.AllowUserToAddRows = false;
            }
        }
    }
